# exception greenlet
import greenlet


def test_greenlet_tracting():
    def callback(event, args):
        print('callback:'+event, 'from', id(args[0]), 'to', id(args[1]))

    def dummy():
        g2.switch()

    def dummyexception():
        raise Exception('dummyexception: excep in coroutine')

    main = greenlet.getcurrent()
    g1 = greenlet.greenlet(dummy)
    g2 = greenlet.greenlet(dummyexception)

    print('main id {},gr1 id:{} ,gr2 id:{} '.format(id(main), id(g1), id(g2)))
    oldtrace = greenlet.settrace(callback)

    try:
        g1.switch()
    except:
        print("Exception")
    finally:
        greenlet.settrace(oldtrace)


if __name__ == "__main__":
    test_greenlet_tracting()
